//
//  ImageTableViewCell.m
//  CloudMD
//
//  Created by apple on 16/3/2.
//  Copyright (c) 2016年 HuaZhengInfo. All rights reserved.
//

#import "ImageTableViewCell.h"
#import "SDWebImageManager.h"

@implementation ImageTableViewCell

//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
//    
//    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        
//        [_imageArray removeAllObjects];
//        NSUserDefaults * infoUser = [NSUserDefaults standardUserDefaults];
//        _imageArray = [infoUser objectForKey:@"INFO"];
//        
//        
//        [self buildUI];
//    }
//    return self;
//}



- (void)setImageArray:(NSMutableArray *)imageArray {
    NSArray *subViewArray = self.contentView.subviews;
    for (int i = 1; i < subViewArray.count; i ++) {
        [subViewArray[i] removeFromSuperview];
    }
    if (!self.label) {
        self.label = [GDFactory creatUILabelWithFrame:CGRectMake(10, 15, self.frame.size.width, self.frame.size.height * 0.2) text:@"资料:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
        [self.contentView addSubview:self.label];
    }
    
    CGFloat margin = 5;
    CGFloat startX = 10;
    CGFloat imageW = SCR_W / 6;
    CGFloat imageH = imageW;
    
    if (imageArray.count > 0) {
        
        for (int i = 0; i < imageArray.count; i ++) {
            
            //修改 待合并
            UIImageView *infoImage = [[UIImageView alloc] init];
            infoImage.tag = i;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletap:)];
            [infoImage addGestureRecognizer:tapGesture];
            
            if (i < 5) {
                infoImage.frame = CGRectMake(startX + (imageW + margin) * i, CGRectGetMaxY(self.label.frame) + 10, imageW, imageH);
            } else {
                infoImage.frame = CGRectMake(startX + (imageW + margin) * (i-5), imageW + self.frame.size.height*0.2 +30, imageW, imageH);
            }
            
            NSString * str = imageArray[i];
            NSURL * url = [NSURL URLWithString:str];
            
            UIActivityIndicatorView * Indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            Indicator.frame = CGRectMake(0,0,SCR_W, SCR_H);
            Indicator.center = CGPointMake(15, 15);
            [infoImage addSubview:Indicator];
            [Indicator startAnimating];
            [[SDWebImageManager sharedManager]downloadImageWithURL:url options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                ;
            }completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                //设置image
                infoImage.image = image;
                [Indicator stopAnimating];
            }];
//            [_infoImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder (1)"]];
            
            //        _infoImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",_imageArray[i]]];
            //        _infoImage.backgroundColor = [UIColor redColor];
            [self.contentView addSubview:infoImage];
        }
    }else{
        
        _imageArray = [NSMutableArray arrayWithObjects:@"nopic", nil];
        UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(startX, CGRectGetMaxY(self.label.frame) + 10, imageW, imageH)];
        NSString * imageName = [_imageArray firstObject];
        [infoImage setImage:[UIImage imageNamed:imageName]];
        [self.contentView addSubview:infoImage];
    }
    
    
}

- (void)handletap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"hello");
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
